package org.vectrics.recruiter.job;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.log4j.Logger;
import org.vectrics.company.Company;
import org.vectrics.user.Person;

public class JobStatusTypeApprovalSet extends HashSet {

	private static final long serialVersionUID = -770942154546574072L;
	
	private transient Logger log = Logger.getLogger(this.getClass());
	
	public JobStatusTypeApprovalSet() {
		super();
	}

	public boolean canPersonTransitionTo(Company company, Person person, JobStatusType jobStatusType, 
			JobStatusType toJobStatusType) {
		boolean result = false;
		
		Set approvals = this.getApprovalsForJobStatusType(jobStatusType);
		if (approvals.size() == 0) {
			result = true;
		} else {
			Iterator iter = approvals.iterator();
			while (iter.hasNext()) {
				JobStatusTypeApproval approval = (JobStatusTypeApproval)iter.next();
				if (approval.getToJobStatusType().getCode().equals(toJobStatusType.getCode())) {
					if (company.isPersonInGroup(person, approval.getCompanyPersonGroupType())) {
						result = true;
					}
				} else {
					log.debug("Approval doesn't match 'toJobStatusType' ");
				}
			}
		}
		return(result);
	}
	
	public Set getApprovalsForJobStatusType(JobStatusType fromJobStatusType) {
		Set approvals = new HashSet();
		Iterator iter = this.iterator();
		while (iter.hasNext()) {
			JobStatusTypeApproval approval = (JobStatusTypeApproval)iter.next();
			if (fromJobStatusType == null) {
				if (approval.getFromJobStatusType() == null) {
					approvals.add(approval);
				}
			} else {
				if (approval.getFromJobStatusType() != null) {
					if (approval.getFromJobStatusType().getCode().equals(fromJobStatusType.getCode())) {
						approvals.add(approval);
					}
				}
			}
		}
		return(approvals);
	}
	
}
